<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Poll/classes/PhpFox_ComponentAdd.class.php, $Author:  $ */



/** Component for adding new poll
 *
 * @version $Id: PhpFox_ComponentAdd.class.php 46 2007-12-14 23:12:21Z phpfoxsv $
 * @package module.poll
 */
class PhpFox_Mod_Poll_ComponentAdd extends BaseComponent
{
    function process()
    {
    	App::membersOnly();

        $oPollItem = &$this->_oModule->getItem('Poll');
        /* @var $oPollItem PhpFox_Mod_Poll_ItemPoll */
        $oSecurityService = &App::getModuleService('Account', 'Security');
        /* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
        $oAccountService = &App::getModuleService('Account', 'Account');
        /* @var $oAccountService PhpFox_Mod_Account_ServiceAccount */
        $oTpl = &$this->_oModule->getTemplate();
        $oReq = &App::getRequest();

        App::loadClass('Text');
        $oTxt = &new Text();

        $oSecurityService->checkPerm('poll', 'full');
        $oSecurityService->checkAccess('poll');
        $sUser = $oSecurityService->getCurrentUserLogin();

        if ($iPollId = $oReq->getInt('id'))
        {
            $oPollItem->load($iPollId);
            if ($sUser != $oPollItem->aData['user'])
            {
                App::gotoUrl('public.poll');
            }
            $oTpl->assign(array(
                'bAdded' => true,
                'aPoll'  => $oPollItem->aData,
            ));
            return $oTpl->fetch('Add.html');
        }
        if ($oReq->getAction() == 'add')
        {
            $sQuestion = $oReq->get('question');
            if (!$sQuestion)
            {
                $this->addErrors(App::format('Mod_Poll.add.no_question'));
            }
            $aAnswers = $oReq->getArray('answer');
            foreach ($aAnswers as $iKey => $sAnswer)
            {
                if (!$sAnswer)
                {
                    unset($aAnswers[$iKey]);
                }
            }
            $aAnswers = array_values($aAnswers);
            if (!count($aAnswers) || (count($aAnswers) < 2))
            {
                $this->addErrors(App::format('Mod_Poll.add.no_answers'));
            }
            if (!count($this->getErrors()))
            {
                $oPollItem->aData['poll'] = $oTxt->preParseText($sQuestion);
                $oPollItem->aData['user'] = $sUser;
                $oPollItem->aData['time'] = time();
                for ($i = 0; $i <= 3; $i++)
                {
                    if (isset($aAnswers[$i]))
                    {
                        $sKey = 'o';
                        $sKey .= $i + 1;
                        $oPollItem->aData[$sKey] = $oTxt->preParseText($aAnswers[$i]);
                    }
                }
                if (App::getSetting('approve_polls'))
                {
                    $oPollItem->aData['type'] = 1;
                }
                $iPollId = $oPollItem->insert();
                $oAccountService->changeUserInfo($oSecurityService->getCurrentUser(), 'poll');
                App::gotoUrl('public.account.submit.add-poll', array('id' => $iPollId));
            }
            else
            {
                $oTpl->assign(array(
                    'aErrors'   => $this->getErrors(),
                    'aAnswers'  => $aAnswers,
                    'sQuestion' => $sQuestion,
                ));
            }
        }

        return $oTpl->fetch('Add.html');
    }
}
?>